<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
public Map getMap(List<Map> result , Map<String,String> deptMap){
	
		//处理结合数据
		for(Map map : result){
		
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("name",deptMap.get(ksdm));
			
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		
		return resMap;
	}
		// 将map按照时间字段值降序
	class MapComparator implements Comparator<Map> {
		@Override
		public int compare(Map map1, Map map2) {
			double time1 = 0;
			double time2 = 0;
			try {
				time1 = Double.parseDouble(Tools.getDesc(map1.get("xmje").toString().split("%")[0]));
				time2 = Double.parseDouble(Tools.getDesc(map2.get("xmje").toString().split("%")[0]));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return (time1 > time2) ? -1 : (time1 == time2) ? 0 : 1;
		}
	}
%>
<%
	//查询住院药占比科室排名
	String yydm =  input.getDesc("yydm","01");//医院代码	
	String date = input.getDesc("start_date","2018-01-28");//开始时间
	String endDa = input.getDesc("end_date","2018-01-28");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";

	String sqlWhere = " and m.zxrq > '"+ startDate + "' and m.zxrq < '" + endDate+"' " ;
	if(!yydm.equals("-1"))
	sqlWhere += " and m.ksdm in (select id  from YY_KSBMK where yydm ='"+yydm+"') " ;
	
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept  ");
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	
	//住院药占比科室排名

	String sql = "select  m.zje as zje ,m.dxmdm as ypbz ,m.ksdm as ksdm from ZY_BRFYMXK m  where 1=1 and  m.jlzt in(0,2) " + sqlWhere+ " ";
	
	StatelessSession ss =  DBTools.db("basedb").getSf().openStatelessSession();
       List<Map> feelist = new ArrayList<Map>();
	try {
		 ss.beginTransaction();
		
		 SQLQuery lbsqlQuery = ss.createSQLQuery(sql);
		lbsqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
         feelist = lbsqlQuery.list();
		
		 ss.getTransaction().commit();
		 ss.close();
		
    }catch (Exception e){
	    ss.getTransaction().commit();
		 ss.close();
		  e.printStackTrace();
    }
	//List<Map> feelist = QBuilder.db("basedb").queryHql("select new map( m.zje as zje ,yy.ypbz as ypbz ,js.ksdm as ksdm) from ZY_BRJSK js ,ZY_BRFYMXK m , YY_SFDXMK yy  where js.syxh = m.syxh and yy.id = m.dxmdm and js.jlzt = 0  " + sqlWhere+ " ");

	
	Map<String,String> deptMapMoney = new HashMap<String,String>();//总费用
	Map<String,String>  deptMapYpMoney = new HashMap<String,String>();//药品费用
	//计算科室
	for(Map map : feelist ){
		String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
		
		double deptSumMoney = Double.parseDouble(deptMapMoney.get(ksdm) == null ? "0" : deptMapMoney.get(ksdm).toString());
		double deptSumYpMoney = Double.parseDouble(deptMapYpMoney.get(ksdm) == null ? "0" : deptMapYpMoney.get(ksdm).toString());
		double moneyTemp = Double.parseDouble(map.get("zje").toString());
		int ypbz = Integer.parseInt(map.get("ypbz") == null ? "0" : map.get("ypbz").toString());
		
		deptSumMoney += moneyTemp;
	
		if(ypbz == 1 ||  ypbz == 2 || ypbz == 3){
			deptSumYpMoney += moneyTemp;
			deptMapYpMoney.put(ksdm, ""+deptSumYpMoney);
		}
		//计算科室的费用
		deptMapMoney.put(ksdm, ""+deptSumMoney);
		
	}
	
	DecimalFormat df = new DecimalFormat("######0.00");
	List<Map> result = new ArrayList<Map>();
	for(Map map : deptList){
		String ksdm = map.get("id") == null ? "0" : map.get("id").toString();
		double	deptSumMoney = Double.parseDouble(deptMapMoney.get(ksdm) == null ? "0" : deptMapMoney.get(ksdm).toString());
		double  deptSumYpMoney = Double.parseDouble(deptMapYpMoney.get(ksdm) == null ? "0" : deptMapYpMoney.get(ksdm).toString());
		if(deptMapMoney.get(ksdm) == null || deptSumYpMoney == 0){
			continue;
		}
		//System.out.println(deptSumYpMoney+"=="+deptSumMoney);
		Map<String,String> tempmap = new HashMap<String,String>();
		tempmap.put("ksdm",ksdm);
		deptSumMoney = deptSumMoney == 0 ? 1 : deptSumMoney;
		tempmap.put("xmje",df.format(deptSumYpMoney/deptSumMoney*100)+"%");
		result.add(tempmap);
	}
	//
	Collections.sort(result, new MapComparator());
	Map<String,Object> finalMap = new HashMap<String,Object>();
	finalMap.put("ksMap",getMap(result,deptMap));
	Message msg = new Message();

	msg.put("errcode",0);
	msg.put("zy_charge_ratio_dept",finalMap);

	out.println(msg);
%>